﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using Mimoza.Common;

namespace Mimoza.DBLayer.SQLSC
{
    class SQLCS_DBHost: Mimoza.Common.IDBHostRepository
    {
        private SqlCeConnection connection = null;

        private SQLCS_DBHost()
        { }

        public SQLCS_DBHost(SqlCeConnection conn)
        {
            connection = conn;
        }

        #region IDBHost Members

        Mimoza.Common.HostRecord Mimoza.Common.IDBHostRepository.GetHost(Guid ID)
        {
            SqlCeCommand cmd = null;
            ContainerRecord record = new ContainerRecord();

            try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = SQLCS_Helper.SelectHostsString + " WHERE [ID] = @ID";
                cmd.Parameters.AddWithValue("@ID", ID);

                SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable);                
                string[] columns = rs.ResultSetView.Columns;

                if (!rs.ReadFirst()) return null;

                return SQLCS_Helper.HostRecordFromResultSet(rs, columns);
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           
        }

        Mimoza.Common.HostRecord Mimoza.Common.IDBHostRepository.GetHost(string MACAddress)
        {
            SqlCeCommand cmd = null;
            ContainerRecord record = new ContainerRecord();

            try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = SQLCS_Helper.SelectHostsString + " WHERE [MACAddress] = @MACAddress";
                cmd.Parameters.AddWithValue("@MACAddress", MACAddress);

                SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable);
                string[] columns = rs.ResultSetView.Columns;

                if (!rs.ReadFirst()) return null;

                return SQLCS_Helper.HostRecordFromResultSet(rs, columns);
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           

        }

        Mimoza.Common.HostRecord Mimoza.Common.IDBHostRepository.GetHostByName(string hostName)
        {
            SqlCeCommand cmd = null;
            ContainerRecord record = new ContainerRecord();

            try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = SQLCS_Helper.SelectHostsString + " WHERE [Name] = @Name";
                cmd.Parameters.AddWithValue("@Name", hostName);

                SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable);
                string[] columns = rs.ResultSetView.Columns;

                if (!rs.ReadFirst()) return null;

                return SQLCS_Helper.HostRecordFromResultSet(rs, columns);
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }

        }

        void Mimoza.Common.IDBHostRepository.Insert(Mimoza.Common.HostRecord host)
        {
            SqlCeCommand cmd = null;

            try
            {
                if (host.ID == null || host.ID == Guid.Empty)
                {
                    host.ID = Guid.NewGuid();
                }
                host.ModificationID = Guid.NewGuid();
                host.UpdateDate = DateTime.Now;
                host.LastConnectionDate = DateTime.Now;

                cmd = connection.CreateCommand();

                SQLCS_Helper.GreateInsertCommand(cmd, "Hosts", host);

                cmd.ExecuteNonQuery();

                RaiseOnObjectChanged(new DBObjectChangedEventArgs(host, DBEventType.ObjectInserted));
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }
        }

        void Mimoza.Common.IDBHostRepository.Update(Mimoza.Common.HostRecord host)
        {
            SqlCeCommand cmd = null;

            try
            {
                host.UpdateDate = DateTime.Now;
                if (host.LastConnectionDate == null) host.LastConnectionDate = DateTime.Now;

                cmd = connection.CreateCommand();

                SQLCS_Helper.GreateUpdateCommand(cmd, "Hosts", host);

                cmd.ExecuteNonQuery();

                RaiseOnObjectChanged(new DBObjectChangedEventArgs(host, DBEventType.ObjectUpdated));
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           
        }

        void Mimoza.Common.IDBHostRepository.Delete(Mimoza.Common.HostRecord host)
        {
            SqlCeCommand cmd = null;

            try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = "DELETE FROM Hosts WHERE [ID] = @ID";
                cmd.Parameters.AddWithValue("@ID", host.ID);

                cmd.ExecuteNonQuery();

                RaiseOnObjectChanged(new DBObjectChangedEventArgs(host, DBEventType.ObjectDeleted));
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           
        }

        public event EventHandler<DBObjectChangedEventArgs> OnObjectChanged;

        private void RaiseOnObjectChanged(DBObjectChangedEventArgs e)
        {
            EventHandler<DBObjectChangedEventArgs> handler = OnObjectChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion
    }
}
